package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import po.Client;
import service.ClientService;
import service.impl.ClientServiceImpl;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
    	response.setHeader("content-type","text/html;charset=UTF-8");
		String name = request.getParameter("name");
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		
		Client c = new Client(name, password1);
		
		ClientService clientService = new ClientServiceImpl();
		int m = clientService.addClient(c);
		if(password1.equals(password2)){
			if(m == 1){
				request.setAttribute("success", "注册成功");
				response.sendRedirect("login.jsp");
			}else{
				request.setAttribute("error","注册不成功");
				request.getRequestDispatcher("register.jsp").forward(request, response);
			}
		}else{
			request.setAttribute("error","密码不一致");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		
		
	}

}
